package com.taichuan.selfcheck.ad;

import android.content.Context;
import android.util.Log;


/**
 * Created by OISNY on 2016/7/15.
 */
public class ADViewPresenter {
    private Context mContext;
    private ADArea mADArea;
    private ADViewInterface mADViewInterface;

    private ADVideoPresenter mVideoPresenter;
    private boolean updatedLoad = false;
    // 临时暂停视频广告播放，用于人脸识别时只播放图片广告
    private boolean isStopPlayVideo = false;

    public ADViewPresenter(Context context, ADViewInterface viewInterface, ADArea area) {
        mContext = context;
        mADViewInterface = viewInterface;
        mADArea = area;
        mVideoPresenter = new ADVideoPresenter(this);
    }

    public boolean isUpdatedLoad() {
        return updatedLoad;
    }

    public void setUpdatedLoad(boolean updatedLoad) {
        this.updatedLoad = updatedLoad;
    }

    public void setStopPlayVideo(boolean stop) {
        isStopPlayVideo = stop;
    }

    public Context getContext() {
        return mContext;
    }

    public ADArea getADArea() {
        return mADArea;
    }


    public ADVideoPresenter getVideoPresenter() {
        return mVideoPresenter;
    }

    public void pause() {
        log("pause execute current PlayedType  : " + mADArea.getPlayedType());
        if (mADArea.getPlayedType() == ADArea.PlayedType.VIDEO) {
            dismissSurfaceView();
//            //没有播放
//            mADArea.setPlayedType(ADArea.PlayedType.NONE);
            //隐藏广告控件
            mADViewInterface.dismissADView();
        }

    }

    public void resume() {
        boolean isPlayVideo = isPlayVideo();
        log("resume execute video isPlayVideo : " + isPlayVideo);
        //重置更新状态
        setUpdatedLoad(false);
        mADArea.setPlayedType(ADArea.PlayedType.VIDEO);
        //当前播放
        log("resume execute current playedType judge Video: " + mADArea.getPlayedType());
        if (mADArea.getPlayedType() == ADArea.PlayedType.VIDEO) {
            if (isPlayVideo) {
                playVideo();
                return;
            }
        }
        dismissSurfaceView();
        //没有播放
        mADArea.setPlayedType(ADArea.PlayedType.NONE);
        //隐藏广告控件
        mADViewInterface.dismissADView();
    }



    private boolean isPlayVideo() {
        //是否允许播放视频
        log("resume execute video isPlayVideo in config : " + mADArea.isPlayedVideo());
        if (mADArea.isPlayedVideo()) {
            log("resume execute video isEmpty : " + mVideoPresenter.isEmpty() + " ,isUpdatedLoad : " + isUpdatedLoad());
            if (mVideoPresenter.isEmpty() || isUpdatedLoad()) {
                //加载本地数据
                mVideoPresenter.loadVideoLists();
            }
            //是否有数据
            if (!mVideoPresenter.isEmpty()) {
                return true;
            }
        }
        return false;
    }

    private void playVideo() {
        //执行
        mVideoPresenter.main();
        //显示
        mADViewInterface.showSurfaceView();
    }

    public boolean isPlayAD() {
        return isPlayVideo();
    }

    private void dismissGrallery(boolean isDismissed) {
        if (isDismissed) {
            //隐藏
            mADViewInterface.dismissGrallery();
        }
//        mImagePresenter.cancelTimer();
    }

    private void dismissSurfaceView() {
        //移除
        mADViewInterface.dismissSurfaceView();
    }

    public void reset(ADArea.PlayedType playedType) {
        mADArea.setPlayedType(playedType);
        if (mADViewInterface.isADViewSurvival()) {
            resume();
        }
    }

    private void log(String log) {
        Log.d(getClass().getSimpleName(),
                " #[ ADView ]# " + log);
    }

    public void resetNMImageData() {
        if (mADViewInterface.isADViewSurvival()) {
            setUpdatedLoad(true);
            resume();
        }
    }
}
